﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EntitySpaces.Interfaces;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Fin_Fin003c : AdminBasePage
{
     decimal shipID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }
        if (!IsPostBack)
        {
            FinShipping fs = new yncTech.BusinessObjects.FinShipping();
            fs.LoadByPrimaryKey(shipID);

            if (fs.ShippingStatus != ConstantValue.ShippingStatusCode.Pending)
            {
                btnRemove.Visible = false;
            }
        }

    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    void GridDataBind(int page)
    {

        VBoxReceiptsQuery q1 = new VBoxReceiptsQuery();
        FinShippingBoxesQuery q2 = new FinShippingBoxesQuery();

        q1.SelectAll();
        q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
        q1.Where(q2.ShipID == shipID);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";

            List<decimal> boxIDs = new List<decimal>();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        Label lbReceipt = (Label)item.FindControl("lbBoxID");
                        boxIDs.Add(decimal.Parse(lbReceipt.Text));
                    }
                }

                if (boxIDs.Count > 0)
                {
                    FinShippingBoxesCollection shippingBoxs = new FinShippingBoxesCollection();
                    shippingBoxs.Query.Where(shippingBoxs.Query.BoxID.In(boxIDs.ToArray()) && shippingBoxs.Query.ShipID == shipID);
                    shippingBoxs.Query.Load();
                    shippingBoxs.MarkAllAsDeleted();
                    shippingBoxs.Save();

                    FinBoxCollection finBoxes = new FinBoxCollection();
                    finBoxes.Query.Where(finBoxes.Query.BoxID.In(boxIDs.ToArray()));
                    finBoxes.Query.Load();

                    foreach (var item in finBoxes)
                    {
                        item.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                        item.UpdatedDttm = DateTime.Now;
                        item.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                      

                    }
                    finBoxes.Save();

                    //将盒中的凭证也设置为:待转移

                    FinBoxReceiptsCollection boxRecs = new yncTech.BusinessObjects.FinBoxReceiptsCollection();
                    boxRecs.Query.Where(boxRecs.Query.BoxID.In(boxIDs.ToArray()));
                    boxRecs.Query.Load();

                    FinReceiptCollection receipts = new yncTech.BusinessObjects.FinReceiptCollection();
                    receipts.Query.Where(receipts.Query.ReceiptID.In(boxRecs.Select(r => r.ReceiptID.Value).ToArray()));
                    receipts.Query.Load();
                    foreach (var rec in receipts)
                    {
                        rec.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                        rec.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        rec.UpdatedDttm = DateTime.Now;
                    }
                    receipts.Save();


                }

                tr.Complete();

                GridDataBind(1);
                WebHelper.RefreshOpener();
            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}